<template>
	<view>
		<view class="search-box">
			<!-- 使用 uni-ui 提供的搜索组件 -->
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<view class="item" v-for="(item,index) in query" :key="item.songId" @tap="toRadio(item.songId)">
			<text class="te">{{index+1}} {{item.songName}}
				<text class="t2">- {{item.singerName}}</text>
			</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				query: []
			}
		},
		methods: {
			input(e) {
				console.log(e);
				// 清除 timer 对应的延时器
				clearTimeout(this.timer)
				// 重新启动一个延时器，并把 timerId 赋值给 this.timer
				this.timer = setTimeout(() => {
					// 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
					this.kw = e
					this.getSearchList()
					console.log(this.kw)
				}, 500)
			},
			async getSearchList() {
				// 判断关键词是否为空
				if (this.kw === '') {
					this.searchResults = []
					return
				}
				//发起请求，获取搜索建议列表
				const {
					data: res
				} = await uni.$http.get("/song/findMuhu", {
					str: this.kw
				})
				console.log(res);
				if (res.code != 200)
					uni.showToast({
						duration: 500,
						title: "数据请求失败",
						icon: 'error'
					})
				this.query = res.data
			},
			toRadio(item){
				uni.navigateTo({
					url:"/pages/mus/mus?songId="+item
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
		background-color: #efefef;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}

	.item {
		height: 100rpx;
		display: flex;
		justify-items: center;
		margin: 20rpx 40rpx;
		border: 2rpx solid gray;
		border-radius: 20rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;

		.te {
			margin-left: 20rpx;
			line-height: 100rpx;
			font-size: 50rpx;

			.t2 {
				margin-left: 30rpx;
				font-size: 40rpx;
			}
		}
	}
</style>
